package com.example.hello_world.train_sql;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.hello_world.R;

public class Sqlite extends AppCompatActivity implements View.OnClickListener {

    private mySqliteDatabase dataHelper;
    private Button Btn_insert,Btn_update,Btn_query,Btn_delete;
    private StringBuilder sb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        dataHelper=new mySqliteDatabase(this,"my.db",null,1);
        Btn_insert=findViewById(R.id.insert);
        Btn_update=findViewById(R.id.query);
        Btn_query=findViewById(R.id.update);
        Btn_delete=findViewById(R.id.delete);

        Btn_insert.setOnClickListener(this);
        Btn_update.setOnClickListener(this);
        Btn_query.setOnClickListener(this);
        Btn_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        SQLiteDatabase DB=dataHelper.getWritableDatabase();
        switch (v.getId()) {
            case R.id.insert:
                ContentValues values1=new ContentValues();
                values1.put("name","哈哈哈哈哒");
                DB.insert("person",null,values1);
                Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.query:
                sb=new StringBuilder();
                Cursor cursor=DB.query("Student",null,null,null,null,null,null);
                if (cursor.moveToFirst()) {
                    do{
                        int studentId=cursor.getInt(cursor.getColumnIndex("studentId"));
                        String name=cursor.getString(cursor.getColumnIndex("name"));
                        sb.append("id:"+studentId+" "+"Name"+name);
                    }while(cursor.moveToNext());
                }
                cursor.close();
                break;
            case R.id.update:

                break;
            case R.id.delete:

                break;
        }
    }
}